Fix #13736: don't forget private property expressions of different classes#4896
Merged
ondrejmirtes merged 2 commits into2.1.xfrom Feb 12, 2026
Merged
Fix #13736: don't forget private property expressions of different classes#4896ondrejmirtes merged 2 commits into2.1.xfrom
ondrejmirtes merged 2 commits into2.1.xfrom
Conversation
- When a method with side effects is called, private properties that the method's declaring class cannot access should not be invalidated - Added invalidatingClass parameter to invalidateExpression() and shouldInvalidateExpression() in MutatingScope - Added isPrivatePropertyOfDifferentClass() to check if a property expression refers to a private property declared in a different class than the one whose method triggered the invalidation - New regression test in tests/PHPStan/Analyser/nsrt/bug-13736.php Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a static or instance method is called on a base class, PHPStan was incorrectly invalidating type-narrowed private property expressions of the current class, even though the called method cannot access those properties. For example, calling
self::assertTrue(true)(defined inBaseClass) would forget thatself::$foo(a private property in the child class) had been narrowed from?FootoFoo.Changes
?ClassReflection $invalidatingClassparameter toMutatingScope::invalidateExpression()andMutatingScope::shouldInvalidateExpression()(src/Analyser/MutatingScope.php)MutatingScope::isPrivatePropertyOfDifferentClass()helper method that checks if an expression is a private property fetch (instance or static) whose declaring class differs from the invalidating classNodeScopeResolver.phpto pass$methodReflection->getDeclaringClass()when invalidating expressions after method calls with side effects:tests/PHPStan/Analyser/nsrt/bug-13736.phpCLAUDE.mdwith documentation about the invalidation patternRoot cause
MutatingScope::shouldInvalidateExpression()has special logic that when$thisis being invalidated, it also matches expressions containingself,static,parent, or the current class name — thereby invalidating static and instance property fetches likeself::$fooand$this->foo. However, it did not consider PHP's visibility rules: a method in a parent class cannot access private properties of a child class. The fix adds a check that preserves private property expression types when the invalidating method's declaring class differs from the property's declaring class.Test
The regression test covers:
self::$foo = new Foo()followed byself::assertTrue(true)(inherited from parent) — type should remainFoo, not revert toFoo|null$this->instanceFoo = new Foo()followed byparent::doSomething()— type should remainFoo, not revert toFoo|nullFixes phpstan/phpstan#13736